forval i = 1/3{
	import delimited "$dir/Model/simulated_data/simulated_data_rec_decomp_`i'.csv", clear
	
	//renaming
	ren v1 theta
	ren v2 e
	ren v3 x
	ren v4 a
	ren v5 t
	ren v6 p
	ren v7 I
	ren v8 H 
	ren v9 tau
	ren v10 s
	ren v11 s_val
	ren v12 prob_diff_I
	ren v13 prob_diff_H
	ren v14 l 
	ren v15 m
	ren v16 w
	ren v17 tax
	ren v18 c
	ren v19 util
	ren v20 switch
	ren v21 gov_exp
	ren v22 sim
	ren v23 i_v

	
	drop if t == 0 

	//get lifetime wages and years worked for each individual simulation
	preserve
	collapse (sum) w l tax, by(i_v sim)
	tempfile outcomes
	ren w w_total
	ren l l_total
	ren tax tax_total
	save `outcomes'
	restore

	merge m:1 i_v sim using `outcomes', keep(match) nogen

	keep if t == 23 
	gen enroll = (s>1)
	gen hs = (e==1 & s==1)
	gen drop = (e==1 & s>1)
	gen sc = (e==2)
	gen coll = (e==3)

	su enroll hs drop sc coll w_total l_total tax_total
	gen inc_dec = ceil(i_v / 2)


	//lifetime wages and years worked, average savings and educadtional attainment, by parent income ventile
	collapse (mean) w_total l_total tax_total enroll hs drop sc coll gov_exp, by (inc_dec)
	ds inc_dec, not
	foreach var in `r(varlist)'{
		ren `var' `var'_rec_d`i'
	}
	save "$temp/outcomes_rec_pinc_dec_decomp_`i'", replace

}


//get to work!
use "$temp/outcomes_base_pinc_dec", clear

forval i = 1/3{
	merge 1:1 inc_dec using "$temp/outcomes_rec_pinc_dec_decomp_`i'", keep(match) nogen
}

ds w_total l_total tax_total enroll hs drop sc coll gov_exp
foreach var in `r(varlist)'{
	forval i = 1/3{
		gen `var'_delta_rec_d`i' = `var'_rec_d`i' - `var'
	}
}


forval i = 1/3{
	replace coll_delta_rec_d`i' = coll_delta_rec_d`i' * 100
	replace w_total_delta_rec_d`i' = w_total_delta_rec_d`i' * 40000
	replace w_total_delta_rec_d`i' = w_total_delta_rec_d`i' / 1000
}




//lifetime wage impacts in thousands of dollars

save "$temp/rec_impacts_decomp", replace

twoway connected coll_delta_rec_d1 inc_dec, ylabel(-5(2.5)15) yline(0) xlabel(1(1)10) ///
xtitle("Parent Income Decile") ytitle("Change in College Attainment (pp)") ///
|| connected coll_delta_rec_d2 inc_dec || connected coll_delta_rec_d3 inc_dec, ///
legend(lab(1 "Labor Market Effect") lab(2 "Parental Resource Effect") lab(3 "Educational Policy Effect"))
graph export "$output/recession_impacts_coll_decomp.png", replace


twoway connected w_total_delta_rec_d1 inc_dec, ylabel(-25(25)125) yline(0) xlabel(1(1)10) ///
xtitle("Parent Income Decile") ytitle("Change in Lifetime Wages ($1000)") ///
|| connected w_total_delta_rec_d2 inc_dec || connected w_total_delta_rec_d3 inc_dec, ///
legend(lab(1 "Labor Market Effect") lab(2 "Parental Resource Effect") lab(3 "Educational Policy Effect"))
graph export "$output/recession_impacts_w_decomp.png", replace
graph close




